import pyglet
from pyglet.gui import PushButton

from BaseScene import BaseScene


class StartScene(BaseScene):
    """开始界面场景"""

    def __init__(self, scene_manager):
        super().__init__(scene_manager)

        # logo屏是否显示完毕
        self.is_entered_main_page: bool = False
        # 音乐启动延迟时间
        self.music_start_delay: float = 0.1 #6
        # 开头两张画面的延时
        self.zk_delay: float = 0.1
        self.title_delay: float = 0.1

        # 主界面背景可动图像
        self.background: pyglet.sprite.Sprite|None = None

        # 创建界面元素
        #self.bgzk_sprite=pyglet.sprite.Sprite(self.pic_resources["zk"], x=0, y=0,batch=self.batch,group=self.group0)
        # self.fade_in_picture(self.pic_resources["zk"], 1.0)
        # pyglet.clock.schedule_once(self.start_title_fade, 3.0)
        pyglet.clock.schedule_once(self.music_delay, self.music_start_delay)
        #self.fade_in_picture(self.pic_resources["title"],duration1=1.5)
        #self.play_music(self.muc_resources["title"])

        # 创建按钮 - 使用形状代替图片（实际项目中请使用图片）
        button_width, button_height = 200, 67
        button_x = self.window.width // 2 - button_width // 2

        self.start_button = PushButton(button_x, 10000, self.pic_resources["newbtn1"], self.pic_resources["newbtn0"],self.pic_resources["newbtn2"],batch=self.batch,group=self.group3)  # 先隐藏按钮
        self.start_button.set_handler('on_release', self.on_start_pressed)
        self.buttons.append(self.start_button)

        self.save_button = PushButton(button_x, 10000, self.pic_resources["msaves1"], self.pic_resources["msaves0"],self.pic_resources["msaves2"],batch=self.batch,group=self.group3)
        self.save_button.set_handler('on_release', self.on_save_pressed)
        self.buttons.append(self.save_button)

        # 按钮文本
        # self.start_text = pyglet.text.Label('新游戏', font_name='SimHei', font_size=20,
        #                                     x=self.window.width // 2, y=325,
        #                                     anchor_x='center', batch=self.batch)
        # self.save_text = pyglet.text.Label('存档', font_name='SimHei', font_size=20,
        #                                    x=self.window.width // 2, y=225,
        #                                    anchor_x='center', batch=self.batch)

    def music_delay(self, dt):
        self.play_music("title")
        # 显示主界面按钮
        self.start_button.y = 500
        self.save_button.y = 300
        # 现在认为这里是动画播放完毕的
        self.is_entered_main_page = True
        self.background = pyglet.sprite.Sprite(self.pic_resources["title"], batch=self.batch, group=self.group0)
        self.background.scale = 2
        self.background.x = 0
        self.background.y = 0
        self.window.set_exclusive_mouse(True)

    def start_title_fade(self, dt):
        self.fade_in_picture(self.pic_resources["title"], 2.0)


    def on_start_pressed(self, button):
        """开始按钮按下事件"""
        print("切换到游戏场景")
        # game_scene = GameScene(self.scene_manager,0)
        self.scene_manager.switch_scene("game", 0)

    def on_save_pressed(self, button):
        """存档按钮按下事件"""
        print("切换到存档场景")
        # save_scene = SaveScene(self.scene_manager,0)
        self.scene_manager.switch_scene("save", 0)

    def on_draw(self):
        """绘制开始场景"""
        self.window.clear()
        self.batch.draw()

    def on_key_press(self, symbol, modifiers):
        """键盘事件处理"""
        pass

    def on_mouse_motion(self, x: int, y, dx, dy):
        if self.is_entered_main_page:
            self.background.x = self.background.x + dx
            self.background.y = self.background.y + dy
